import 'dart:io';

import 'package:cc_sdk/models/comment_list_result.dart';
import 'package:cc_sdk/models/comment_model.dart';
import 'package:cc_sdk/models/pager_result.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';

class _ServiceException implements Exception {
  final String message;

  const _ServiceException(this.message);
}

class CommentService {
  //final _baseUrl = 'http://res.test.hsq168.co:9200/';
  final _baseUrl = 'http://sns.hsq168.vip/';
  static final CommentService _internal = CommentService.internal();

  factory CommentService() => _internal;

  CommentService.internal();

  Future<T> _request<T, M>(String url,
      {Map<String, dynamic> data, M convert(dynamic item)}) async {
    try {
      var response = await Dio().request(_baseUrl + url,
          data: data,
          options: RequestOptions(
              contentType:
                  ContentType.parse("application/x-www-form-urlencoded"),
              method: data == null ? "GET" : "POST"));
      if (response.statusCode == 200) {
        if (response.data["errorCode"] == 0) {
          var result = response.data["data"];
          if (result.runtimeType.toString().startsWith("List<")) {
            var list =
                (result as List<dynamic>).map((item) => convert(item)).toList();
            return list as T;
          } else {
            if (convert != null) {
              return convert(result) as T;
            } else {
              return result;
            }
          }
        } else {
          throw _ServiceException(response.data["errorMsg"]);
        }
      } else {
        throw _ServiceException('服务器错误');
      }
    } on _ServiceException catch (e) {
      Fluttertoast.showToast(msg: e.message);
      throw e;
    } on Exception catch (e) {
      Fluttertoast.showToast(msg: "网络请求发生错误");
      throw _ServiceException('网络请求发生错误');
    }
  }

  Future<CommentModel> comment(
      String outId, int parentId, int memberId, String context) async {
    var result = await _request<CommentModel, CommentModel>('Api/Comment',
        data: {
          "memberId": memberId,
          "parentId": parentId,
          "context": context,
          "outId": outId
        },
        convert: (dynamic item) => CommentModel.fromMap(item));
    return result;
  }

  Future<CommentListResult> list(
      String outId, int parentId, int pageIndex, int memberId) async {
    var result =
        await _request<CommentListResult, CommentListResult>('Api/List',
            data: {
              "outId": outId,
              "parentId": parentId,
              "memberId": memberId,
              "pageIndex": pageIndex
            },
            convert: (dynamic item) => CommentListResult.fromMap(item));
    return result;
  }

  Future like(String outId, int memberId) async {
    var result = await _request<CommentModel, CommentModel>(
      'Api/Like',
      data: {"memberId": memberId, "outId": outId},
    );
    return result;
  }

  Future unlike(String outId, int memberId) async {
    var result = await _request<CommentModel, CommentModel>(
      'Api/UnLike',
      data: {"memberId": memberId, "outId": outId},
    );
    return result;
  }
}
